<?php
session_start();
require_once("../includes/dbconfig.php");
require_once("../includes/functions.php");
require_once("../includes/settings_postbacks.php");


$oid = makesafe($_REQUEST['campid']);
$status = makesafe($_REQUEST['status']);
$subID = makesafe($_REQUEST['sid']);
$network = 'Adscendmedia';

$validIps = array('72.52.162.102', '199.59.164.3', '199.59.164.5');

$subID = explode("-",$subID);
$uid = $subID[0];
$hash = $subID[1];
$isGW = $subID[2];

$_GET['ip'] = getIP();

$ip = getIP();

if(!in_array($ip, $validIps))
{
    die("You are not allowed to use this page!");	
}



if(!empty($isGW))
{
	
	$gid = $uid;
	//if gid is defined, we just have to use gw postbacks.

    require_once("../assets/HSTGw__x_offer_postbacks/adscendmedia_postback.php");
	return;
	
} 
 
 

if(empty($uid) || empty($oid) || empty($hash))
{
  die("Missing required variables");
}




$user = getUserById($uid);

if(!$user)
{
  die("Invalid Username");
}
//get Referrer Id ..
$ref_id = getReferrerId($uid);

//get Offer Process Details
$sql = mysql_query("SELECT * FROM offer_process WHERE uid = '$uid' AND code = '$hash' AND `campaign_id` = '$oid' AND network = '$network' LIMIT 1");
if(!mysql_num_rows($sql))
{
  die("Invalid Offer Process");
}

//get Offer Info
$sql2 = mysql_query("SELECT * FROM offers WHERE campaign_id = '$oid' AND active = 1 AND network = '$network' LIMIT 1");
if(!mysql_num_rows($sql2))
{
    die("Invalid Offer");
}




$row = mysql_fetch_object($sql);
$points = $row->credits;
$ref_points = $row->ref_credits;
$file_id = $row->file_id;

$offerName = makesafe($row->offer_name);
$offer_id  = $r1->offer_id;




$currentStatus = $row->status;

if($currentStatus == 2)
return;


if($status == 2) //Status is not 1 it means offer not approved.
{
	
	if($currentStatus == 1)
	{

		@mysql_query("UPDATE earnings_log SET credits = '0.00', notes = 'Reversed (regular -$points)', date = NOW() WHERE uid = '$uid' AND src_offer_id = '$oid' AND network = '$network' AND hash = '$hash' AND notes LIKE 'Offer%'");  //revoke user credit log	
		@mysql_query("UPDATE users SET balance = balance-".$points." WHERE uid='".$uid."'");
		@mysql_query("UPDATE offers SET leads = leads-1, hits=hits-1 WHERE campaign_id = '$oid' AND network = '$network' LIMIT 1");
		//update epc
		setEpc($oid, $network);			
		
		if($ref_id && !empty($ref_points) && $ref_points >= 0.01)
		{
			@mysql_query("UPDATE users SET balance = balance-".$ref_points." WHERE uid='".$ref_id."'");
		     @mysql_query("UPDATE earnings_log SET credits = '0.00', notes = 'Reversed (referral -$ref_points)', date = NOW() WHERE uid = '$ref_id' AND src_offer_id = '$oid' AND network = '$network' AND hash = '$hash' AND notes LIKE 'Referral%'");  //revoke user credit log	
			 @mysql_query("DELETE FROM `referral_income_log` WHERE campid = '$oid' AND network = '$network' AND  hash = '$hash' AND referrer_id = '$ref_id' AND  referral_id = '$uid')");
		
		
		}		

	}

		
    mysql_query("UPDATE offer_process SET status = 2 WHERE campaign_id = '$oid' AND code = '$hash' AND network = '$network' AND status != 2 AND uid = $uid");
   	echo "Offer not approved";
	   exit;
}


if($currentStatus == 1)
return;




mysql_query("UPDATE users SET balance = balance+".$points." WHERE uid='".$uid."'") or die("Error occured while crediting user");
mysql_query("INSERT INTO `earnings_log` VALUES(NULL, '$uid', '$file_id', '0', '$oid', '$offerName', '$points', 'Offer Lead', NOW(), '$network', '$hash')") or die("Error occured while adding user earning log");


mysql_query("UPDATE offer_process SET status = 1 WHERE code = '$hash' AND campaign_id = '$oid' AND network = '$network' AND status = 0 AND uid = $uid");
//add leads count
@mysql_query("UPDATE offers SET leads = leads+1 WHERE campaign_id = '$oid' AND network = '$network' LIMIT 1");

$token = $hash;
@mysql_query("INSERT INTO ready_downloads VALUES(NULL, '$token', '$file_id', NOW(), 'regular')");

//update epc
setEpc($oid, $network);





//Add referral comission to referrer's account
if($ref_id && !empty($ref_points) && $ref_points > 0)
{
    mysql_query("UPDATE users SET balance = balance+$ref_points WHERE uid='".$ref_id."'") or die("Error occured while crediting referrer");
	mysql_query("INSERT INTO `earnings_log` VALUES(NULL, '$ref_id', '0', '$uid', '$oid', NULL, '$ref_points', 'Referral Income', NOW(), '$network', '$hash')") or die("Error occured while adding referral commission log");
	mysql_query("INSERT INTO `referral_income_log` VALUES(NULL, '$uid', '$ref_id', '$ref_points', NOW(), '$oid', '$network', '$hash')");	


}


/*$token = md5(strtotime('now'))."-".strtotime('now').md5(uniqid()).sha1(strtotime('now').uniqid());

while(mysql_num_rows(mysql_query("SELECT hash FROM ready_downloads WHERE hash = '$token'")))
{
	$token = md5(strtotime('now'))."-".strtotime('now').md5(uniqid).sha1(strtotime('now').uniqid());
}*/


/*
$_SESSION['file_download_token_'.$file_id] = $token;
if(isset($_SESSION['file_download_token_'.$file_id])){
@mysql_query("INSERT INTO ready_downloads VALUES(NULL, '$token', '$file_id', NOW(), 'regular')");
}

*/
mysql_close();
echo "Success: ".$uid." earned ".$points." credits\n  Referral Commision $ref_points added to referrer"; 




exit;

?>